#ifndef GCRITSECWRAPPER_H_INCLUDED
#define GCRITSECWRAPPER_H_INCLUDED

class CGCriticalSectionWrapper
{
public:
#if GLIB_CHECK_VERSION(2, 32, 0)
  CGCriticalSectionWrapper() { g_rec_mutex_init(&m_mutex); }
  ~CGCriticalSectionWrapper() { g_rec_mutex_clear(&m_mutex); }
  void Enter() { g_rec_mutex_lock(&m_mutex); }
  void Leave() { g_rec_mutex_unlock(&m_mutex); }
#else
  CGCriticalSectionWrapper() { m_mutex = g_mutex_new(); }
  ~CGCriticalSectionWrapper() { g_mutex_free(m_mutex); }
  void Enter() { g_mutex_lock(m_mutex); }
  void Leave() { g_mutex_unlock(m_mutex); }
#endif

private:
#if GLIB_CHECK_VERSION(2, 32, 0)
  GRecMutex m_mutex;
#else
  GMutex *m_mutex;
#endif
};

class CGAutoLock
{
public:
  CGAutoLock(CGCriticalSectionWrapper *lock) { m_lock = lock; m_lock->Enter(); }
  ~CGAutoLock() { m_lock->Leave(); };

protected:
  /* COPY CONSTRUCTOR NOT ACCESSIBLE */
  CGAutoLock(const CGAutoLock &refAutoLock);
  CGAutoLock & operator=(const CGAutoLock &refAutoLock);

private:
  CGCriticalSectionWrapper *m_lock;
};

#endif // GCRITSECWRAPPER_H_INCLUDED
